/**
 * small and fast plug to load the scripts 
 * which have dependencies asynchronously
 */
var script = {};
(function(load){
	var models = {};
	//a map dictionary records the 'script' request
	var reqWorks = {};
	load.define = function(url,dependencies,callback){
		var model = {};
		if(null == url || url == ''){
			throw new Error("'url/id' can't not be empty");
		}
		if(null == models[url]){
			model.url = url;
			if(null != dependencies){
				if(dependencies instanceof Array){
					model.dependencies = dependencies;
					for(var array in dependencies){
						var key = dependencies[array];
						if(typeof(models[key])=="undefined"){
							throw new Error("can't not find the dependency named "+key);
						}else{
							models[key].parents.push(url);
						}
					}
				}else{
					throw new Error(dependencies+" should be a Array!");
				}
			}
			model.parents = [];
			model.callback = callback;
			model.isLoad = false;
			models[url]=model;
		}
	};
	load.load = function(url){
		if(typeof(models[url])=="undefined"){
			throw new Error("can't load the script which url is "+url);
		}
		var model = models[url];
		var reqWork = {};
		reqWork.url = url;
		reqWork.dependencies = model.dependencies;
		reqWork.parents = model.parents;
		reqWork.complete = false;
		reqWorks[url]=reqWork;
		prepareLoad(model);
	};
	function prepareLoad(model){
		if(null != model.dependencies){
			var dependencies = model.dependencies;
			for(var array in dependencies){
				load.load(dependencies[array]);
			}
		}else{
			load0(model);
		}
	};
	function load0(model){
		if(!model.isLoad){
			model.isLoad = true;
			var head = document.head||document.getElementsByTagName("head")[0];
			var script0 = document.createElement("script");
			script0.type = "text/javascript";
			if(script0.readyState){
				script0.onreadystatechange = function(){
					if(script0.readyState=="loaded"||script0.readyState=="complete"){
						if(null != model.callback){
							model.callback();
						}
						updateReqWork(model.url);
					};
				};
			}else{
				script0.onload=function(){
					if(null != model.callback){
						model.callback();
					}
					updateReqWork(model.url);
				};
			}
			script0.src = model.url;
			head.appendChild(script0);
		}
	};
	function updateReqWork(url){
		var reqWork = reqWorks[url];
		reqWork.complete = true;
		for(var i in reqWork.parents){
			var parent = reqWork.parents[i];
			var parentWork = reqWorks[parent];
			if(typeof(parentWork) == "undefined"){
				continue;
			}
			var silblingWorks = parentWork.dependencies;
			var flag = 1;
			for(var j = 1;j<silblingWorks.length;i++){
				if(!siblingWorks[j].complete){
					flag = 0;
					break;
				}
			}
			if(flag == 1){
				load0(models[parent]);
			}
		}
	};
	function host(){
		var host0 = window.location.host;
		//will be?
		if(host0 == null){
			host0 = document.domain;
		}
		return host0;
	};
})(script);
